package com.lanswon.qzsmk.service.systemManageService.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lanswon.qzsmk.base.CardContants;
import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.base.HttpContants;
import com.lanswon.qzsmk.dao.AddressAreaMapper;
import com.lanswon.qzsmk.dao.AddressCityMapper;
import com.lanswon.qzsmk.dao.AddressProvinceMapper;
import com.lanswon.qzsmk.dao.AddressStreetMapper;
import com.lanswon.qzsmk.dao.AppKindMapper;
import com.lanswon.qzsmk.dao.BusiParamMapper;
import com.lanswon.qzsmk.dao.CardLossMapper;
import com.lanswon.qzsmk.dao.CardPhysicsTypeMapper;
import com.lanswon.qzsmk.dao.CardProdunctDefinitionMapper;
import com.lanswon.qzsmk.dao.CardTypeMapper;
import com.lanswon.qzsmk.dao.CertificateMapper;
import com.lanswon.qzsmk.dao.NationMapper;
import com.lanswon.qzsmk.dao.PaperTypeMapper;
import com.lanswon.qzsmk.model.ActiveUser;
import com.lanswon.qzsmk.model.BusiParam;
import com.lanswon.qzsmk.model.CardProdunctDefinition;
import com.lanswon.qzsmk.model.CardType;
import com.lanswon.qzsmk.service.systemManageService.CardParameterManagementService;
import com.lanswon.qzsmk.vo.BusiParamVo;
import com.lanswon.qzsmk.vo.CardProdunctVo;
import com.lanswon.qzsmk.vo.ResultObject;
import com.lanswon.qzsmk.vo.ResultStatus;
import com.lanswon.qzsmk.vo.ResultVo;

import ch.qos.logback.classic.Logger;

/**
 * 新增履历 工程项目名称：青州市民卡核心系统 方法名：卡参数管理模块-CardParameterManagementImpl 版本： ver 1.0 初版
 * 创建日期：20190319 创建人：鱼洲杰
 */
@Service
public class CardParameterManagementServiceImpl implements CardParameterManagementService {

	private final static Logger logger = (Logger) LoggerFactory.getLogger(CardParameterManagementServiceImpl.class);

	@Autowired
	CardPhysicsTypeMapper cptm;

	@Autowired
	CardProdunctDefinitionMapper cpdm;

	@Autowired
	PaperTypeMapper ptm;

	@Autowired
	CardTypeMapper ctm;

	@Autowired
	CertificateMapper cm;

	@Autowired
	AddressAreaMapper aam;

	@Autowired
	AddressStreetMapper asm;

	@Autowired
	AddressProvinceMapper apm;

	@Autowired
	AddressCityMapper acm;

	@Autowired
	NationMapper nm;

	@Autowired
	CardLossMapper clm;

	@Autowired
	AppKindMapper akm;

	@Autowired
	CardTypeMapper ct;

	@Autowired
	BusiParamMapper bpm;

	@Override
	public void AddProductTask(CardProdunctVo cpv) throws Exception {
		logger.info("AddProductTask########start()");
		ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getSession().getAttribute("activeUser");
		if (activeUser == null) {
			activeUser = new ActiveUser();
			activeUser.setUserName("用户");
			activeUser.setLoginName("用户");
			activeUser.setBranchName("互联网端");
			activeUser.setBranchNo("99999999");
			activeUser.setUserNo(99999999);
			activeUser.setUserId(99999999);
		}
		CardProdunctDefinition ct = new CardProdunctDefinition();
		Integer newID =null; 
		String ID = cpdm.queryMaxCardProductId();
		if("".equals(ID) || ID==null){
				newID=1;
		}else{
			newID=Integer.parseInt(ID)+1;;
		}
		
		ct.setId(newID);
		ct.setCardpCode(cpv.getCardpCode());// 卡产品代码
		ct.setCardpName(cpv.getCardpName());// 卡产品名称
		ct.setCardTypeCode(cpv.getCardTypeCode());
		// 卡类型代码
		ct.setCardPublisherCode("10");
		// 卡出版商代码
		ct.setHasChips(CardContants.CARD_HAS_CHIPS);// 是否有接触芯片
		ct.setHasChipsLess(CardContants.CARD_HAS_CHIPS_LESS);// 是否有非接触芯片
		ct.setHasBankStripe(CardContants.CARD_NO_HAS_BANK_STRIPE);// 是否有银行磁条
		ct.setHasBarcode(CardContants.CARD_NO_HAS_BARCODE);// 是否有条形码
		ct.setHasPassword(CardContants.CARD_HAS_PASSWORD);// 是否有密码
		ct.setDefaultPw("123456");// 缺省密码
		ct.setCardValidTime(cpv.getCardValidTime());// 卡片有效期
		ct.setInitOrgCode(cpv.getInitOrgCode());// 初始化机构代码
		ct.setChipsVer("");// 接触版本号
		ct.setChipsLessVer("");// 非接触版本号
		ct.setCityCode(cpv.getCityCode());// 城市代码
		ct.setValidStatus(cpv.getValidStatus());// 有效标志
		ct.setDescription(cpv.getDescription());// 描述
		ct.setCrtOperator(cpv.getCrtOperator());// 创建柜员号
		ct.setCrtDate(cpv.getCrtDate());// 创建日期
		ct.setLastOperator(cpv.getLastOperator());// 最后修改柜员号
		ct.setLastDate(cpv.getLastDate());// 最后修改日期
		ct.setDefCardConsumer(cpv.getDefCardConsumer());// 默认卡消费方
		ct.setIsCustomCard(cpv.getIsCustomCard());// 是否是定制卡
		ct.setLibraryAppFlag(cpv.getLibraryAppFlag());// 图书馆应用
		ct.setBusAppFlag(cpv.getBusAppFlag());// 公交应用
		ct.setBicycleAppFlag(cpv.getBicycleAppFlag());// 自行车应用
		ct.setSmallConsumptionAppFlag(cpv.getSmallConsumptionAppFlag());// 小额消费应用
		ct.setCardProperty(cpv.getCardProperty());// 小额消费应用
		cpdm.insert(ct);
		logger.info("AddProductTask########end()");
	}


	@Override
	public List<CardType> queryAllCardType() {

		return ct.queryAllCardType();
	}

	@Override
	public void modifyCardTaskByCityCode(CardProdunctVo cpv) {
		logger.info("deleteProduct########start()");
		ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getSession().getAttribute("activeUser");
		if (activeUser == null) {
			activeUser = new ActiveUser();
			activeUser.setUserName("用户");
			activeUser.setLoginName("用户");
			activeUser.setBranchName("互联网端");
			activeUser.setBranchNo("99999999");
			activeUser.setUserNo(99999999);
			activeUser.setUserId(99999999);
		}
		cpv.setLastOperator(activeUser.getUserNo().toString());
		cpv.setLastDate(new Date());
		cpdm.modifyCardTaskByCityCode(cpv);
		logger.info("deleteProduct########end()");

	}

	@Override
	public CardProdunctDefinition queryCardProductByCode(String CardTypeCode) throws Exception {

		return cpdm.queryCardCode(CardTypeCode);
	}

	@Override
	public CardProdunctDefinition queryCardProductByID(String id) {

		return cpdm.queryCardProdunctByID(id);
	}

	@Override
	public ResultVo queryAllCardProdunct(CardProdunctVo cpv) {
		logger.info("queryAllCardProdunct########start()");
		ResultVo rv = new ResultVo();
		ResultStatus rs = new ResultStatus();
		// 获取卡产品定义信息
		List<CardProdunctDefinition> list = cpdm.queryAllCardProdunct(cpv);
		// 返回画面的list，显示画面表格内容
		logger.debug("listSize==>" + list.size());
		List<CardProdunctVo> l = new ArrayList<>();

		// 卡类型信息
		CardType cardTypeObject = new CardType();

		for (CardProdunctDefinition c1 : list) {
			CardProdunctVo c2 = new CardProdunctVo();
			c2.setCardpCode(c1.getCardpCode());
			// 卡类型名称
			cardTypeObject = ctm.queryCardTypeByCode(c1.getCardTypeCode());
			c2.setId(c1.getId());
			c2.setCardTypeName(cardTypeObject.getCardTypeName());
			c2.setCardpName(c1.getCardpName());

			c2.setCardTypeCode(c1.getCardTypeCode());
			c2.setValidStatus(c1.getValidStatus());
			c2.setCardValidTime(c1.getCardValidTime());
			c2.setCityCode(c1.getCityCode());
			c2.setBusAppFlag(c1.getBusAppFlag());
			c2.setBicycleAppFlag(c1.getBicycleAppFlag());
			c2.setLibraryAppFlag(c1.getLibraryAppFlag());
			c2.setSmallConsumptionAppFlag(c1.getSmallConsumptionAppFlag());
			c2.setCrtDate(c1.getCrtDate());
			c2.setCrtOperator(c1.getCrtOperator());
			c2.setLastDate(c1.getLastDate());
			c2.setLastOperator(c1.getLastOperator());
			c2.setCardpCode(c1.getCardpCode());
			c2.setCardProperty(c1.getCardProperty());
			l.add(c2);
		}
		DwzPage<CardProdunctVo> dp = new DwzPage<CardProdunctVo>(cpv.getCurrentPage(), cpv.getNumPerPage(), l.size());
		
		dp.setList(l);
		
		dp.setList(dp.getPagedList());
		
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");

		rv.setResultStatus(rs);
		rv.setServiceObject(dp);
		logger.info("queryAllCardProdunct########end()");
		return rv;
	}

	@Override
	public void startProduct(CardProdunctDefinition cpv) {
		logger.info("startProduct########start()");
		ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getSession().getAttribute("activeUser");
		if (activeUser == null) {
			activeUser = new ActiveUser();
			activeUser.setUserName("用户");
			activeUser.setLoginName("用户");
			activeUser.setBranchName("互联网端");
			activeUser.setBranchNo("99999999");
			activeUser.setUserNo(99999999);
			activeUser.setUserId(99999999);
		}
		cpv.setValidStatus(CardContants.STATUS_ENABLE);
		cpv.setLastOperator(activeUser.getUserNo().toString());
		cpv.setLastDate(new Date());
		cpdm.startProduct(cpv);
		logger.info("startProduct########end()");
	}

	@Override
	public ResultVo queryAllBusiParam(BusiParamVo bpv) {
		logger.info("queryAllBusiParam########start()");
		ResultVo rv = new ResultVo();
		ResultStatus rs = new ResultStatus();
		List<BusiParamVo> list = bpm.queryAllBusiParam(bpv);
		/*List<BusiParamVo> l = new ArrayList<>();
		for (BusiParam c1 : list) {
			BusiParamVo c2 = new BusiParamVo();
			c2.setCardpName(c1.getCardpName());
			c2.setCardpCode(c1.getCardpCode());
			c2.setCustomerNo(c1.getCustomerNo());
			c2.setIsLoss(c1.getIsLoss());
			c2.setAllowCoc(c1.getAllowCoc());
			c2.setAllowChc(c1.getAllowChc());
			c2.setIsCardReturn(c1.getIsCardReturn());
			c2.setCrtDate(c1.getCrtDate());
			c2.setCrtOperator(c1.getCrtOperator());
			c2.setLastDate(c1.getLastDate());
			c2.setLastOperator(c1.getLastOperator());
			l.add(c2);
		}*/

		int totalCount = bpm.queryAllCardBusiParamNumber(bpv);
		DwzPage<BusiParamVo> dp = new DwzPage<BusiParamVo>(bpv.getCurrentPage(), bpv.getNumPerPage(), totalCount);
		dp.setList(list);
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");

		rv.setResultStatus(rs);
		rv.setServiceObject(dp);
		logger.info("queryAllBusiParam########end()");
		return rv;

	}

	@Override
	public void addBusPara(BusiParamVo bpv) throws Exception {
		logger.info("addBusPara########start()");
		ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getSession().getAttribute("activeUser");
		if (activeUser == null) {
			activeUser = new ActiveUser();
			activeUser.setUserName("用户");
			activeUser.setLoginName("用户");
			activeUser.setBranchName("互联网端");
			activeUser.setBranchNo("99999999");
			activeUser.setUserNo(99999999);
			activeUser.setUserId(99999999);
		}
		BusiParam bp = new BusiParam();
		//bp.setCardpName(bpv.getCardpName());
		bp.setCardpCode(bpv.getCardpCode());// 卡产品代码
		bp.setCustomerNo("0");// 客户唯一持有
		bp.setIsLoss("0");// 是否允许挂失
		bp.setAllowCoc("0");// 是否允许补卡
		bp.setCocChangeNo("");// 补卡是否换卡号
		bp.setCocCodeChange("");// 补卡条形码是否变化
		bp.setAllowChc("0");// 是否允许换卡
		bp.setChcChangeNo("");// 换卡卡号是否变化
		bp.setChcCodeChange("");// 换卡条形码是否变化
		bp.setIsCardReturn("0");// 是否可退卡
		bp.setRemark("");// 备注
		bp.setCrtOperator(activeUser.getUserNo().toString());// 创建柜员号
		bp.setCrtDate(new Date());// 创建日期
		bp.setLastOperator("");// 最后修改柜员号
		bp.setLastDate(bpv.getLastDate());// 最后修改日期
		bp.setCocRegNewcard("");// 补卡登记新卡是否产生新卡
		bp.setCocRegNewcardPre("");// 补卡登补卡登记是否由预制卡生成
		bp.setRecycleOldcard("");// 是否回收旧卡（换卡 退卡）
		bp.setChcRegNewcard("");// 换卡登记是否尝产生新卡
		bp.setChcRegNewcardPre("");// 换卡登记新卡是否由预制卡生成
		bp.setNewcardPreCardp("");// 使用预制卡卡产品代码
		bp.setReleaseDays(Integer.valueOf(0));// 领卡天数
		bp.setChcReleaseDays(Integer.valueOf(0));// 换卡领卡天数
		bp.setCocReleaseDays(Integer.valueOf(0));// 补卡领卡天数
		bp.setIsCardOutstorage("");// 发卡是否库存出库
		bp.setChcAuditneed("");// 换卡审批参数
		bp.setCocAuditneed("");// 补卡审批参数
		bp.setRecAuditneed("");// 退卡审批参数
		bp.setChcTransferBalance("");// 换卡现场发卡是否转余额
		bp.setCocTransferBalance("");// 补卡现场发卡是否转余额
		bp.setRecRefundDay(Integer.valueOf(3));// 注销退款约定日期
		bp.setChcTransferBalanceDays(Integer.valueOf(0));// 换卡余额转移约定天数
		bp.setCocTransferBalanceDays(Integer.valueOf(0));// 补卡余额转移约定天数
		bp.setIsCardnoMag("");// 是否需要卡号段管理
		bp.setIsExportExtrafile("");// 导出制卡文件时是否导出附加文件
		logger.info("addBusPara########end()");
		bpm.insert(bp);

	}


	@Override
	public void modifyCardBusParaByCityCode(BusiParamVo bpv) {
		logger.info("modifyCardBusParaByCityCode########start()");
		ActiveUser activeUser = null;
		try {
			activeUser = (ActiveUser) SecurityUtils.getSubject().getSession().getAttribute("activeUser");

		} catch (UnavailableSecurityManagerException e) {
			e.printStackTrace();
			activeUser = new ActiveUser();
			activeUser.setUserName("用户");
			activeUser.setLoginName("用户");
			activeUser.setBranchName("互联网端");
			activeUser.setBranchNo("99999999");
			activeUser.setUserNo(99999999);
			activeUser.setUserId(99999999);
		}

		if (activeUser == null) {
			activeUser = new ActiveUser();
			activeUser.setUserName("用户");
			activeUser.setLoginName("用户");
			activeUser.setBranchName("互联网端");
			activeUser.setBranchNo("99999999");
			activeUser.setUserNo(99999999);
			activeUser.setUserId(99999999);
		}
		bpv.setLastDate(new Date());
		bpv.setLastOperator(activeUser.getUserNo().toString());
		bpm.modifyCardBusParaByCityCode(bpv);
		logger.info("modifyCardBusParaByCityCode########end()");
	}

	@Override
	public BusiParamVo queryCardBusiParamByCode(String CardTypeCode) throws Exception {

		return bpm.queryCardBusiParamByCardPCode(CardTypeCode);
	}

	@Override
	public CardProdunctDefinition queryCardProductByName(String cardpName) {

		return cpdm.queryCardProductByName(cardpName);
	}


	@Override
	public List<CardProdunctDefinition> queryCardProductByIdS(List idList) {

		return cpdm.queryCardProdunctByIDS(idList);
	}

	@Override
	public CardProdunctDefinition queryCardProductById(String id) {
		// TODO Auto-generated method stub
		return cpdm.queryCardProdunctByID(id);
	}
	@Override
	public ResultVo queryAllCardProdunctDefinition() {
		ResultVo rv = new ResultVo();
		ResultStatus rs = new ResultStatus();
		CardProdunctVo cpv=new CardProdunctVo();
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		rv.setResultStatus(rs);

		ResultObject ro = new ResultObject();
		ro.setItems(cpdm.queryAllCardProdunct(cpv));
		rv.setServiceObject(ro);
		return rv;
	}
}